﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Transport;
using System.Threading;
using System.Security.Cryptography;

namespace Transport
{
    public partial class Register : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            lblResult.Visible = false; 
        }
        protected int isExistsUser(string username)
        {
            NguoiDungControl ngDungControl = new NguoiDungControl();
            nguoi_dung userInfo = new nguoi_dung();
            userInfo  = ngDungControl.findByUsername(username);
            if (userInfo != null)
                return 1;
            return 0;
        }
        protected int isExistsEmail(string email)
        {
            NguoiDungControl ngDungControl = new NguoiDungControl();
            nguoi_dung userInfo = new nguoi_dung();
            userInfo = ngDungControl.findByEmail(email);
            if (userInfo != null)
                return 1;
            return 0;
        }
        protected int captchaMatch(string captcha)
        {
            lblResult.Visible = true;
            if (!recaptcha.IsValid)
            {
                return 0;
            }
            else
            {
                return 1;
            }
        }
        protected bool doRegister(string tendangnhap, string hoten, string namsinh, string cmnd, string email, string dt, string diachi, string fax, string cty, string chucdanh, string password)
        {
            nguoi_dung ngDung;
            NguoiDungControl ngDungControl = new NguoiDungControl();
            ngDung = new nguoi_dung();
            ngDung.loai_nguoi_dung = "NORMAL";
            ngDung.ten_dang_nhap = tendangnhap;
            ngDung.ho_ten = hoten;
            ngDung.nam_sinh = DateTime.Parse(namsinh);
            ngDung.cmnd = cmnd;
            ngDung.email = email;
            ngDung.sdt = dt;
            ngDung.dia_chi = diachi;
            ngDung.fax = fax;
            ngDung.cong_ty = cty;
            ngDung.chuc_danh = chucdanh;
            HashAlgorithm alg = new MD5CryptoServiceProvider();
            byte[] bte = alg.ComputeHash(System.Text.Encoding.UTF8.GetBytes(password));
            ngDung.password = BitConverter.ToString(bte).Replace("-", "").ToLower();

            bool result = ngDungControl.insert(ngDung);
            if (result)
                return true;
            return false;
        }
    }
}